﻿using Miao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine.AddressableAssets;
using Miao;
using UnityEngine;

namespace Miao.UI
{

    public static class UIAddressableInit
    {
        readonly static string[] prefabPath = { "Assets\\Plugins\\miao-framework\\UIPrefab" };

        [MenuItem("Miao/置入Miao.UI内置预制的UI")]
        public static void Init()
        {
            AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;


            string[] guids = AssetDatabase.FindAssets("t:Prefab", prefabPath);




            foreach (string guid in guids)
            {
                // 获取预制体的路径和名称
                string path = AssetDatabase.GUIDToAssetPath(guid);
                string name = System.IO.Path.GetFileNameWithoutExtension(path);//文件名但没有后缀
                                                                               // 创建或移动一个 AddressableAssetEntry ，并将其地址设置为预制体的名称
                AddressableAssetEntry addressableAssetEntry = settings.CreateOrMoveEntry(guid, settings.DefaultGroup, false, false);
                addressableAssetEntry.address = addressableAssetEntry.AssetPath;

                Type type = Type.GetType(name);
                if (type == null)
                {
                    continue;
                }
                SourcesPathAttribute sourcesPathAttribute = type.GetCustomAttribute<SourcesPathAttribute>();
                if (sourcesPathAttribute == null)
                {
                    Debug.LogError($"{path}未包含预制体寻址");
                    continue;
                }
                addressableAssetEntry.address = sourcesPathAttribute.path;

            }
            settings.SetDirty(AddressableAssetSettings.ModificationEvent.BatchModification, null, true, true);



            AssetDatabase.SaveAssets();
            //settings.DefaultGroup



            //AddressableAssetGroup.

            //Addressables
        }
    }
}

